package com.joyoung.sweep.activity.login;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.SPStaticUtils;
import com.joyoung.sweep.R;
import com.joyoung.sweep.base.BaseActivity;
import com.joyoung.sweep.config.Cons;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: ThinkPad
 * date: 2021/4/22
 * desc: 引导页
 */
public class GuideActivity extends BaseActivity {

    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.tv_jump)
    TextView tvJump;
    private List<View> views = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        for (int i = 0; i < 4; i++) {
            View view = View.inflate(mContext, R.layout.item_guide_page_view, null);
            ImageView imageView = view.findViewById(R.id.iv_pic);
            imageView.setBackgroundResource(R.mipmap.ic_launcher);
            views.add(view);
        }
        GuidePageAdapter adapter = new GuidePageAdapter();
        vp.setAdapter(adapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position + 1 == views.size()) {
                    tvJump.setVisibility(View.VISIBLE);
                } else {
                    tvJump.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick(R.id.tv_jump)
    public void onViewClicked() {
        SPStaticUtils.put(Cons.FIRST,false);
        openAct(LoginActivity.class);
        finish();
    }

    public class GuidePageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(views.get(position));
        }
    }
}